package br.com.amadeus.android.adapters;


import java.util.List;

import br.com.amadeus.android.activities.home.R;
import br.com.amadeus.android.business.FacadeAmadeusDroid;
import br.com.amadeus.android.util.SchedulerSyncronize;

import android.app.AlarmManager;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class GoalAdapter extends BaseAdapter
{
	private Context ctx;  
	private List<GoalList> goals;  
	FacadeAmadeusDroid facade;
	AlarmManager alarme;
	int course_id;

	public GoalAdapter(Context ctx, List <GoalList> goals, AlarmManager alarme, int course_id) 
	{  
		this.ctx = ctx;  
		this.goals = goals; 
		this.facade = FacadeAmadeusDroid.getInstance();
		this.alarme = alarme;
		this.course_id = course_id;
	}  

	public int getCount() {  
		return goals.size();  
	}  

	public Object getItem(int position) {  
		return goals.get(position);  
	}  

	public long getItemId(int position) {  
		return goals.get(position).getId();  
	}  

	public View getView(int position, View convertView, ViewGroup parent){ 
		
		LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(R.layout.amadeus_goal_item, null);
		
		//atribuindo a objetivo o objeto do arraylist da posição position
		final GoalList goal = goals.get(position);  
		
		TextView tvDefinition = (TextView) v.findViewById(R.id.tvDefinition);
		tvDefinition.setText(goal.getNome());
		ImageButton imgEditar = (ImageButton) v.findViewById(R.id.imgEditGoal);
		ImageButton imgExcluir = (ImageButton) v.findViewById(R.id.imgDeleteGoal);
		
		imgEditar.setOnClickListener(new View.OnClickListener() 
		{
			public void onClick(View v) 
			{
				SchedulerSyncronize.sendBroadcast(1, ctx, alarme, "RECEIVER_FORMGOAL",(int)goal.getId());
			}
		});
		
		imgExcluir.setOnClickListener(new View.OnClickListener() 
		{
			public void onClick(View v) 
			{
				AlertDialog.Builder dialog = new AlertDialog.Builder(ctx);
				dialog.setMessage("Tem certeza que deseja excluir esse registro?");
				
				dialog.setNegativeButton("Não", new DialogInterface.OnClickListener() 
				{
					public void onClick(DialogInterface dialog, int which) 
					{
						
					}
				});

				dialog.setPositiveButton("Sim", new DialogInterface.OnClickListener() 
				{
					public void onClick(DialogInterface dialog, int which) 
					{
						facade.deleteGoal(goal.getId());
						SchedulerSyncronize.sendBroadcast(1, ctx, alarme, "RECEIVER_HOMEGOAL", course_id);
					}
				});
				
				dialog.show();
			}
		});
		
		return v;  
	}  
}
